package com.example.goodpet.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.MapActivity;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.DistanceUtils;
import com.example.goodpet.utils.MyApplication;

import java.util.ArrayList;

public class PetHospitalAdapter extends DelegateAdapter.Adapter<PetHospitalAdapter.ViewHolder> {
    private final Context mContext;
    private final ArrayList<PoiItem> pois;
    private String TAG=this.getClass().getSimpleName();
    //用来获取布局
    private LayoutInflater mLayoutInflater;

    public PetHospitalAdapter(Context mContext, ArrayList<PoiItem> pois) {
        this.mContext=mContext;
        this.pois=pois;
        mLayoutInflater = LayoutInflater.from(mContext);
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PetHospitalAdapter.ViewHolder(mLayoutInflater.inflate(R.layout.item_pet_hospital, null));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//根据位置获取相应的信息
        PoiItem poiItem = pois.get(position);

//        Log.e(TAG, "getView: "+poiItemV2.getPhotos().get(0) );
        if(poiItem.getPhotos().isEmpty()){
            Glide.with(mContext)
                    .load(R.drawable.goodpet_logo)
                    .into(holder.iv_pet_hospital);
        }else{
            Glide.with(mContext)
                    .load(poiItem.getPhotos().get(0).getUrl())
                    .into(holder.iv_pet_hospital);
        }
//        Log.e(TAG, "onBindViewHolder: "+poiItem.getTitle());
        holder.title_pet_hospital.setText(poiItem.getTitle());



        if(null==poiItem.getPoiExtension().getmRating() || "".equals(poiItem.getPoiExtension().getmRating())){
            holder.point_pet_hospital.setText("暂无评分");

        }else{
            holder.point_pet_hospital.setText("评分："+poiItem.getPoiExtension().getmRating());
        }
        if(null==poiItem.getTel() || "".equals(poiItem.getTel())){
            holder.tel_pet_hospital.setText("暂无联系电话");
        }else{
            holder.tel_pet_hospital.setText("联系电话："+poiItem.getTel());
        }

        if(null==poiItem.getPoiExtension().getOpentime() || "".equals(poiItem.getPoiExtension().getOpentime())){
            holder.time_pet_hospital.setText("营业时间未知");
        }else{
            holder.time_pet_hospital.setText(poiItem.getPoiExtension().getOpentime());
        }
        //具体地点
        holder.address_pet_hospital.setText(poiItem.getCityName()+poiItem.getAdName()+
                poiItem.getBusinessArea()+poiItem.getSnippet());

        LatLonPoint latLonPoint = poiItem.getLatLonPoint();

        double distance = DistanceUtils.getDistance(MyApplication.longitude, MyApplication.latitude
                , latLonPoint.getLongitude(), latLonPoint.getLatitude());

        if(distance<1){
            holder.distance_pet_hospital.setText("距离您"+((int)(distance*1000))+"m");
        }else{
            holder.distance_pet_hospital.setText("距离您"+distance+"km");
        }
        holder.item_hospital.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, MapActivity.class);
                intent.putExtra("poiItem",poiItem);
                mContext.startActivity(intent);
            }
        });

//        Log.e(TAG, "getView: "+poiItemV2.getIndoorData().getFloorName()+"\n"
//                +poiItemV2.getBusiness().getBusinessArea()+"\n"+poiItemV2.getBusiness().getTag()+"\n"
//                +poiItemV2.getBusiness().getAlias()+"\n"+poiItemV2.getBusiness().getCost());

    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return pois==null?0:pois.size();
    }


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return null;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_pet_hospital;
        private TextView title_pet_hospital;
        private TextView point_pet_hospital;
        private TextView tel_pet_hospital;
        private TextView time_pet_hospital;
        private TextView address_pet_hospital;
        private TextView distance_pet_hospital;
        private LinearLayout item_hospital;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_pet_hospital=itemView.findViewById(R.id.iv_pet_hospital);
            title_pet_hospital=itemView.findViewById(R.id.title_pet_hospital);
            point_pet_hospital=itemView.findViewById(R.id.point_pet_hospital);
            tel_pet_hospital=itemView.findViewById(R.id.tel_pet_hospital);
            time_pet_hospital=itemView.findViewById(R.id.time_pet_hospital);
            address_pet_hospital=itemView.findViewById(R.id.address_pet_hospital);
            distance_pet_hospital=itemView.findViewById(R.id.distance_pet_hospital);
            item_hospital=itemView.findViewById(R.id.item_hospital);
        }
    }
}
